package com.test.fundible.fundible_06_loan_apply;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.test.fundible.R;
import com.test.fundible.fundible_01_launcher.Base_fundibleApplication;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;

public class LoanApplyDetail_OldUser {

    private static class Inner_fundible {
        private static LoanApplyDetail_OldUser instance_fundible = new LoanApplyDetail_OldUser();
    }

    private LoanApplyDetail_OldUser() {}

    public static LoanApplyDetail_OldUser getInstance_fundible() {
        return Inner_fundible.instance_fundible;
    }

    public static void parse_fundible(JSONObject obj) {
        Inner_fundible.instance_fundible = new Gson().fromJson(obj.toString(), LoanApplyDetail_OldUser.class);

        Inner_fundible.instance_fundible.detailItemList.clear();
        JSONArray param = obj.optJSONArray("detailItemList");
        Gson gson = new Gson();
        if (param!=null){
            for (int i=0;i<param.length();i++){
                try {
                    Inner_fundible.instance_fundible.detailItemList.add(gson.fromJson(param.optString(i), LnoaAp_fundiblel_fundibleyItem.class));
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }

    }

    private String amount;//贷款金额
    private String days;//贷款天数
    private String currency;//货币单位
    private String chooseAmountFlag;//是否可选金额
    private String maxAmount;//最大可选金额
    private String minAmount;//最小可选金额
    private int[] daysArray;//可选天数列表
    private String granularity;//金额选择颗粒度

    //申请页显示参数列表
    private final ArrayList<LnoaAp_fundiblel_fundibleyItem> detailItemList = new ArrayList<>();


    public ArrayList<LnoaAp_fundiblel_fundibleyItem> getDetailItemList_fundible() {
        return detailItemList;
    }

    public int getAmount() {
        if (TextUtils.isEmpty(amount)) return 0;
        try {
            return Integer.parseInt(amount);
        }catch (NumberFormatException e){
            e.printStackTrace();
            return 0;
        }
    }

    public String getAmountWithCurrency(){
        return getAmount()+" "+getCurrency();
    }

    public int getDays() {
        if (TextUtils.isEmpty(days)) return 0;
        try {
            return Integer.parseInt(days);
        }catch (NumberFormatException e){
            e.printStackTrace();
            return 0;
        }
    }

    public String getDaysWithUnit(){
        try {
            int dayI = Integer.parseInt(days);
            if (dayI <= 1) return Base_fundibleApplication.getInstant().getString(R.string.day_fundible,dayI);
            else return Base_fundibleApplication.getInstant().getString(R.string.days_fundible,dayI);
        }catch (NumberFormatException e){
            e.printStackTrace();
            return Base_fundibleApplication.getInstant().getString(R.string.day_fundible,0);
        }
    }

    public String getCurrency() {
        if (TextUtils.isEmpty(currency)) return "";
        return currency;
    }

    public boolean getChooseAmountFlag() {
        if (TextUtils.isEmpty(chooseAmountFlag)) return false;
        return chooseAmountFlag.equalsIgnoreCase("true");
    }

    public String getMaxAmount() {
        return maxAmount;
    }

    public int getMaxAmountNum() {
        try {
            return Integer.parseInt(maxAmount);
        }catch (NumberFormatException e){
            e.printStackTrace();
            return 0;
        }
    }

    public String getMinAmount() {
        return minAmount;
    }

    public int getMinAmountNum(){
        try {
            return Integer.parseInt(minAmount);
        }catch (NumberFormatException e){
            e.printStackTrace();
            return 0;
        }
    }

    public int[] getDaysArray() {
        return daysArray;
    }

    public String getGranularity() {
        return granularity;
    }

    public int getGranularityNum(){
        int result = 0;
        try {
            result = Integer.parseInt(granularity);
            if (result<=0) result = 1;
            return result;
        }catch (NumberFormatException e){
            e.printStackTrace();
            return 1;
        }
    }

    public ArrayList<LnoaAp_fundiblel_fundibleyItem> getDetailItemList() {
        return detailItemList;
    }
}
